Skip to main content
This example demonstrates how to evaluate polynomials homomorphically using PVAC-HFHE. You can compute polynomial functions on encrypted values without decrypting them.

Overview

Polynomial evaluation is a fundamental operation in homomorphic encryption with applications in:
  • Private function evaluation
  • Approximating non-linear functions
  • Machine learning activation functions
  • Statistical computations

Basic polynomial: f(x) = x³ + 2x² + 3x + 4

Let’s evaluate a cubic polynomial at x = 5:
1

Encrypt the input and coefficients

First, encrypt the input value and any coefficients needed:
2

Compute powers of x

Calculate x², x³ homomorphically:
3

Evaluate the polynomial

Combine the terms using homomorphic operations:
You can also write this more compactly as a single nested expression.
4

Decrypt and verify

Decrypt the result and verify correctness:

Complete example

Here’s the full code for evaluating f(x) = x³ + 2x² + 3x + 4:

Optimizing polynomial evaluation

Using Horner’s method

For better efficiency, use Horner’s method to reduce the number of multiplications:
Horner’s method reduces circuit depth and the number of operations, improving performance for high-degree polynomials.

Using constant multiplication

When coefficients are public, use ct_mul_const for better performance:

Higher-degree polynomials

Computing high powers efficiently

Use repeated squaring for efficient power computation:
The circuit depth grows logarithmically with the exponent when using repeated squaring, making it practical to compute high powers.

Circuit depth analysis

Different evaluation strategies result in different circuit depths:
The L.size() field shows the number of layers (circuit depth), while E.size() shows the total number of edges in the computation graph.

Nested expressions

Evaluate complex nested expressions:

Multivariate polynomials

Evaluate polynomials with multiple variables:

Applications

Activation functions in ML

Polynomials can approximate non-linear activation functions:

Private threshold functions

Evaluate comparison thresholds privately:

Performance considerations

Circuit depth optimization: Keep polynomial degree low to minimize circuit depth. For high-degree polynomials, consider using Horner’s method or approximating with lower-degree polynomials.
Coefficient optimization: Use ct_mul_const for public coefficients instead of encrypting them. This reduces both computation time and circuit size.
Batching: When evaluating the same polynomial on multiple inputs, reuse intermediate results where possible.

Source code

The polynomial evaluation example is part of the basic usage tests:
  • examples/basic_usage.cpp (lines 137-148)

Next steps

Basic usage

Learn the fundamentals of PVAC-HFHE

ML credit scoring

Apply polynomials in encrypted ML models